﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ClearTypeSwitch
{
    public class Switcher
    {
        public enum FE
        {
            FE_FONTSMOOTHINGORIENTATIONBGR = 0x0000,
            FE_FONTSMOOTHINGORIENTATIONRGB = 0x0001
        }

        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SystemParametersInfo(SPI uiAction, uint uiParam, out uint pvParam, uint fWinIni);

        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SystemParametersInfo(SPI uiAction, uint uiParam, uint pvParam, uint fWinIni);

        public void Switch()
        {
            uint current;

            SystemParametersInfo(SPI.SPI_GETFONTSMOOTHINGORIENTATION, 0, out current, 0);
            if(current == (uint)FE.FE_FONTSMOOTHINGORIENTATIONBGR)
                SystemParametersInfo(SPI.SPI_SETFONTSMOOTHINGORIENTATION, 0, (uint)FE.FE_FONTSMOOTHINGORIENTATIONRGB, 0);
            else
                SystemParametersInfo(SPI.SPI_SETFONTSMOOTHINGORIENTATION, 0, (uint)FE.FE_FONTSMOOTHINGORIENTATIONBGR, 0);
        }

        public FE Current
        {
            get
            {
                uint current;
                SystemParametersInfo(SPI.SPI_GETFONTSMOOTHINGORIENTATION, 0, out current, 0);

                return (FE)current;
            }
        }
    }
}
